local CommonOHOH = _G.CommonOHOH;
-- local M = CommonOHOH.class.new(script, CommonOHOH.files.BaseComponent(),script.Parent.Name.."_"..script.Name);
local M = CommonOHOH.class.new(script, CommonOHOH.files.BaseComponent(),script.Parent.Name);

local Tool=require(script.Parent.tool) 



function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);

     self.player=self.parent
     self.DataBase=self.parent:GetComponent(_G.ServerOHOH.load.Server_PlayerDataComponent())



    self.isBattle=false


     self.player.BattleSateEvent:Connect(function(isBattle,battle)
        self.isBattle=isBattle
        if not self.isBattle then --退出战斗，恢复血量
            self:UpdateHp()
        end
    end)

    self.player.HpPointChangeEvent:Connect(function() --防御点数发生变化
            self:UpdateHp()
    end)

    self.player.LevelChangeEvent:Connect(function() --等级发生变化
        self:UpdateHp()
    end)


    self.player.HpValueBaseEvent:Connect(function() --计算血量
            return  Tool.GetLevelBaseHp(self.level)
    end)


    self.exp=self.DataBase:GetData("base").exp
    self.level=self.DataBase:GetData("base").level
    self.maxHp=Tool.CalculateMaxHp(self.player)
    self.hp= self.maxHp
    
    self:Update()
    self:UpdateHp()
end;


function M.calculateNextLevel(self,levelId,exp)
    local need=Tool.UpgradeExp(levelId)
    if exp>=need then
        levelId=levelId+1
        exp=exp-need
        return self:calculateNextLevel(levelId,exp)
    else
        return levelId,exp
    end
end



-- 加经验
function M.AddExp(self,add)
    assert(add,"add==nil")
    local nlevel,nexp=self:calculateNextLevel(self.level,self.exp+add)
    self.exp= nexp
    local isChange=false
    local oldLvel= self.level
    if self.level~=nlevel then
        self.level=nlevel
        isChange=true
       
    end
    -- local need=Tool.UpgradeExp(self.level)
    -- if self.exp>=need then --可以升级了
    --     self.exp=self.exp-need
    --     self.level=self.level+1
    --     self.player.LevelChangeEvent:Action()
    -- end
    self:Update()
    self:Save(self.exp,self.level) --保存数据
    if isChange then self.player.LevelChangeEvent:Action(nlevel,oldLvel) end
end



function M.Update(self)
    -- 通知客户端
    self.player.attribute.Level= self.level
    self.player.attribute.Exp= self.exp
end

-- 受伤
function M.Hurt(self,damge)
    self.hp=self.hp-damge
    if self.hp<0 then
        self.hp=0
    end
    self.player.attribute.Hp= self.hp
end

-- 保存属性数据
function M.Save(self,exp,level)
    local baseStore= self.DataBase:GetData("base")
    self.DataBase:Execut(function()
        baseStore.exp=exp
        baseStore.level=level
    end);

    self.player.UserDataEvent:Action("base")
end

--重生
function M.Rebirth(self,newLevel)
    local oldLevel=self.level
    self.exp=0
    self.level=newLevel
    self.player.LevelChangeEvent:Action(newLevel,oldLevel)
    self:Update()
    self:Save(self.exp,self.level) --保存数据
end

-- 更新血量数据
function M.UpdateHp(self)
    local newMaxHP=Tool.CalculateMaxHp(self.player)
    local hpScale= self.hp/self.maxHp
    self.maxHp=newMaxHP
    if  self.isBattle then -- 战斗中
        self.hp= math.floor(self.maxHp*hpScale)
        if self.hp> self.maxHp then
            self.hp=self.maxHp
        elseif self.hp<1 then
            self.hp=1
        end
    else
        self.hp= self.maxHp
    end
    self.player.attribute.Hp=self.hp
    self.player.attribute.MaxHp= self.maxHp --重新计算血量
end




function M.dtor(self)
	M.super.dtor(self);
end

return M